package com.topsail.batch.demo.example10.config;

import com.topsail.batch.demo.example10.InputOverviewDemoItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

/**
 * @author Steven
 * @date 2020-04-05
 */
@Configuration
public class InputOverviewDemoJobConfiguration {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job inputOverviewDemoJob() {
        return jobBuilderFactory.get("inputOverviewDemoJob")
                .start(inputOverviewDemoStep())
                .build();
    }

    private Step inputOverviewDemoStep() {
        return stepBuilderFactory.get("inputOverviewDemoStep")
                .<String, String>chunk(2)
                .reader(inputOverviewDemoReader())
                .writer(list -> {
                    for (String item : list) {
                        System.out.println("-> current item: " + item);
                    }
                })
                .build();
    }


    private InputOverviewDemoItemReader inputOverviewDemoReader() {
        List<String> data = Arrays.asList("one", "two", "three");
        return new InputOverviewDemoItemReader(data);
    }

}
